package com.crr.servlet;

import com.alibaba.fastjson.JSON;
import com.crr.mapper.EmpMapper;
import com.crr.pojo.Emp;
import com.crr.util.JsonUtil;
import com.crr.util.PageUtil;
import com.crr.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/showPage")
public class ShowPageServlet extends HttpServlet {
    SqlSession session = SqlSessionFactoryUtil.getSqlSession();
    EmpMapper em = session.getMapper(EmpMapper.class);

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNum = Integer.parseInt(req.getParameter("pageNum"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        String ename=req.getParameter("ename");
        int count=em.getEmpCount();
        int tp=PageUtil.getTotalPages(count,pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("pageNum", pageSize * (pageNum - 1));
        map.put("pageSize", pageSize);
        map.put("ename",ename);
        List<Emp> list = em.getEmpByPage(map);
        Map<String, Object> maps = new HashMap<>();
        maps.put("Emps", list);
        maps.put("pageNum", pageNum);
        maps.put("pageSize", pageSize);
        maps.put("totalpages",tp);
        JsonUtil.getJson(resp, req, maps);



    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
